<?php

namespace admin\controllers;

use Yii;
use common\models\Warehouse;
use common\models\WarehouseSearch;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

use common\component\MenuHelper;
use common\component\Tree;
use common\models\Admin;
use common\models\AuthAssignment;
use common\models\Menu;
use common\models\User;
use common\service\DataService;
use common\models\AuthItem;
use common\service\MenuService;
use common\service\TreeService;
use admin\controllers\common\BaseController;
use yii\filters\AccessControl;
use yii\helpers\ArrayHelper;
use yii\helpers\VarDumper;
use yii\rbac\Assignment;
use common\component\AdCommon;


/**
 * WarehouseController implements the CRUD actions for Warehouse model.
 */
class WarehouseController extends BaseController
{
    /**
     * Lists all Warehouse models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new WarehouseSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Warehouse model.
     * @param string $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Warehouse model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Warehouse();
        $model->created_at = time();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            AdCommon::showmessage('success','数据提交成功！', 'index');
            //return $this->redirect(['view', 'id' => $model->id]);
        }
        $model->is_display= 1;
        $model->sort= 10;
        return $this->render('create', [
            'model' => $model,
            'searchModel'=>new Warehouse()
        ]);
    }

    /**
     * Updates an existing Warehouse model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        $model->updated_at = time();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            AdCommon::showmessage('success','数据修改成功！', 'update?id='.$model->id);
            //return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
            'searchModel'=>new Warehouse()
        ]);
    }

    /**
     * Deletes an existing Warehouse model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Warehouse model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return Warehouse the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Warehouse::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException(Yii::t('app', 'The requested page does not exist.'));
    }
}
